﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using EpiNEXT.Services;
using EpiNEXT.Services.Fields;
using EpiNEXT.Services.Validation;

namespace EpiNEXT.Services.Validation.Functions
{
    public abstract class FunctionBase : IFunction
    {
        public FunctionBase()
            : base()
        {
            Parameters = new Collection<Token>();
        }

        [XmlArray("Parameters")]
        [XmlArrayItem("Token", typeof(Token))]
        public Collection<Token> Parameters { get; set; }

        public abstract object Execute(Record record);
        public virtual object Evaluate(Record record, params object[] parameters)
        {
            return null;
        }

        public List<IField> GetFields(DataEntryInstrument form)
        {
            List<IField> fields = new List<IField>();

            foreach (Token t in Parameters)
            {
                fields.AddRange(t.GetFields(form));
            }

            return fields;
        }
    }
}
